package com.whut.whw.post;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.github.chrisbanes.photoview.PhotoView;
import com.whut.whw.R;
import com.whut.whw.base.BaseActivity;
import com.whut.whw.util.LogUtils;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by 张芳容 on 2017/06/19.
 */

public class ShowPicActivity extends BaseActivity {
    float mPosX = 0, mPosY = 0, mCurrentPosX = 0, mCurrentPosY = 0;
    final int RIGHT = 0;//向右滑
    final int LEFT = 1;//向左滑
    final int OTHER = -1;//其他
    private GestureDetector gestureDetector;//监听手势的方向
    private ArrayList<String> imagePaths;//所有图片路径
    private int position;
    @BindView(R.id.photo_view)
    PhotoView photo_view;

    @Override
    public int getLayoutId() {
        return R.layout.activity_show_pic;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        try {
            imagePaths = new ArrayList<String>();
            initImage();
            // gestureDetector = new GestureDetector(ShowPicActivity.this,onGestureListener);
        } catch (Exception e) {
            LogUtils.e("显示图片异常", e.toString());
        }

    }

    private void initImage() {
        //ToastUtils.showShort("显示图片");
        imagePaths = getIntent().getExtras().getStringArrayList("imagePaths");
        if ("null".equals(imagePaths.get(imagePaths.size() - 1))) {
            imagePaths.remove(imagePaths.size() - 1);//移除最后一张图片
        }
        position = getIntent().getExtras().getInt("position");
        // ToastUtils.showShort("显示图片"+imagepath);
        showImage();
    }

    private void showImage() {
        Bitmap bitmap = BitmapFactory.decodeFile(imagePaths.get(position));
        photo_view.setImageBitmap(bitmap);
    }

    @Override
    public void setListener() {
        //  photo_view.setOnClickListener(new ImageViewOnClickListener());
        photo_view.setOnTouchListener(new ImageViewOnTouchListener());
    }

    class ImageViewOnClickListener implements PhotoView.OnClickListener {

        @Override
        public void onClick(View v) {
            finish();
        }
    }

    class ImageViewOnTouchListener implements PhotoView.OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                // 按下
                case MotionEvent.ACTION_DOWN:
                    // ToastUtils.showShort("按下啦");
                    mPosX = event.getX();
                    mPosY = event.getY();
                    break;
                // 移动
                case MotionEvent.ACTION_MOVE:
                    //  ToastUtils.showShort("移动啦");
                    mCurrentPosX = event.getX();
                    mCurrentPosY = event.getY();
                    if (mCurrentPosX - mPosX > 0 && Math.abs(mCurrentPosY - mPosY) < 10) {
                        // ToastUtils.showShort("移动啦--右");
                        doResult(RIGHT);
                    } else if (mCurrentPosX - mPosX < 0 && Math.abs(mCurrentPosY - mPosY) < 10) {
                        // ToastUtils.showShort("移动啦--左");
                        doResult(LEFT);
                    } else
                        doResult(OTHER);
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                default:
                    break;
            }
            return true;
        }


        public void doResult(int action) {
            switch (action) {
                case RIGHT:
                    //  System.out.println("go right");
                    position = (position + 1) % (imagePaths.size());
                    break;
                case LEFT:
                    // System.out.println("go left");
                    position = (position - 1 + imagePaths.size()) % (imagePaths.size());
                    break;
                case OTHER:
                    // finish();
                    break;
            }
            showImage();
        }
    }
}
